#include "stdafx.h"
#include "PoomData.h"
#include <initguid.h>
#include "CommonWM.h"

#ifdef _DEBUG
#include "crtdbg.h"
#endif

HWND PoomData::hWnd = NULL;
IPOutlookApp2* PoomData::polApp = NULL;

bool PoomData::Initialize(HWND hWindow)
{
  if (polApp != NULL)
    return true;

  hWnd = hWindow;

  // Now, let's get the main outlook application
  if (FAILED(CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER, 
      IID_IPOutlookApp2, reinterpret_cast<void **>(&polApp))))
    return false;

  // login to the Pocket Outlook object model
  if(FAILED(polApp->Logon((long)hWnd)))
  {
    polApp->Release();
    polApp = NULL;
    // can't login to the app
    return false;
  }

  return true;
}

void PoomData::Terminate()
{
  hWnd = NULL;

  if (polApp)
  {
    polApp->Logoff();
    polApp->Release();
    polApp = NULL;
  }
}

bool PoomData::SubscribeToNotifications(OlDefaultFolders olFolder, UINT uiNotificationsType)
{
  if (polApp == NULL)
  {
    return false;
  }

  HRESULT   hr;
  IFolder   *pFolder = NULL;
  IItem     *pItem   = NULL;
  CEPROPVAL propval  = {0};

  // Get the folder for the item type.
  hr = polApp->GetDefaultFolder(olFolder, &pFolder);
  if (FAILED(hr))
  {
    return false;
  }

  // Get the IItem interface for the IFolder.
  hr = pFolder->QueryInterface(IID_IItem, (LPVOID*)&pItem);
  if (FAILED(hr))
  {
    pFolder->Release();
    return false;
  }

  // Set the folder's properties.
  propval.propid      = PIMPR_FOLDERNOTIFICATIONS;
  propval.val.ulVal   = uiNotificationsType;
  hr = pItem->SetProps(0, 1, &propval);

  // Release resources.
  pItem->Release();
  pFolder->Release();

  return SUCCEEDED(hr);
}

bool PoomData::CreateContact(const TCHAR* szName, const TCHAR* szNumber)
{
  HRESULT hr = 0;
  IContact * pContact = NULL;
  IItem * pItem = NULL;

  if (FAILED(PoomData::GetApp()->CreateItem(olContactItem, (IDispatch**)&pContact)))
    return false;

  if (szName)
  {
    BSTR bstrName = SysAllocStringLen(szName, _tcslen(szName));
    if (bstrName)
    {
      pContact->put_LastName(bstrName);
      pContact->put_FileAs(bstrName);
      SysFreeString(bstrName);
    }
  }

  if (szNumber)
  {
    BSTR bstrNumber = SysAllocStringLen(szNumber, _tcslen(szNumber));
    if (bstrNumber)
    {
      pContact->put_MobileTelephoneNumber(bstrNumber);
      SysFreeString(bstrNumber);
    }
  }

  if (SUCCEEDED(pContact->QueryInterface(IID_IItem, (LPVOID*)&pItem)))
  {
    hr = pItem->Edit(NULL);
    pItem->Release();
    if (hr == S_FALSE || FAILED(hr))
    {
      pContact->Release();
      return false;
    }
  }

  pContact->Release();
  return true;
}

bool PoomData::DeleteContact(CEOID oId)
{
  HRESULT hr;
  IDispatch* pDisp = NULL;
  IItem* pItem = NULL;

  // GET CURRENT CONTACT
  if (FAILED(PoomData::GetApp()->GetItemFromOid(oId, &pDisp)))
      return false;

  if (SUCCEEDED(pDisp->QueryInterface(IID_IItem, (LPVOID*)&pItem)))
  {
    hr = pItem->Delete();
    pItem->Release();
  }

  pDisp->Release();
  return (SUCCEEDED(hr) ? true : false);
}

bool PoomData::GetItemProperties(IItem* pItem, const CEPROPID* aPropIds, int nPropIds, CEPROPVAL** pPropVals, ULONG& nPropValsSize)
{
  if (!pItem || aPropIds == NULL || nPropIds == 0)
    return false;

  HRESULT hr;

  hr = pItem->GetProps(aPropIds, CEDB_ALLOWREALLOC, nPropIds, pPropVals, &nPropValsSize, ::GetProcessHeap());
  return (SUCCEEDED(hr) && *pPropVals != NULL);
}

bool PoomData::GetItemProperty(IItem* pItem, CEPROPID propId, CEPROPVAL** pPropVal, ULONG& nPropValSize)
{
  if (!pItem)
    return false;

  HRESULT hr;

  hr = pItem->GetProps(&propId, CEDB_ALLOWREALLOC, 1, pPropVal, &nPropValSize, ::GetProcessHeap());
  return (SUCCEEDED(hr) && *pPropVal != NULL);
}

bool PoomData::SetItemProperties(IItem* pItem, CEPROPVAL* pPropVals, WORD nPropVals)
{
  if (!pItem)
    return false;

  HRESULT hr;
  hr = pItem->SetProps(0, nPropVals, pPropVals);
  return SUCCEEDED(hr);
}

bool PoomData::SetItemProperty(IItem* pItem, CEPROPVAL& pPropVal)
{
  if (!pItem)
    return false;

  HRESULT hr;
  hr = pItem->SetProps(0, 1, &pPropVal);
  return SUCCEEDED(hr);
}